/**
 * FileName: PhotoGridActivity.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-3 下午4:43:20
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-3 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.ui.photo;

import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.me.safebox.R;
import com.me.safebox.service.FileConfig;
import com.me.safebox.service.FileEncryptTask;
import com.me.safebox.service.FileHandleThreadPool;
import com.me.safebox.service.FileScanner;
import com.me.safebox.service.IFuture;
import com.me.safebox.ui.MainApp;
import com.me.safebox.ui.adapter.FileEncryptAdapter;

/**
 * @ClassName: FileEncryptActivity
 * @Description:文件加密
 * @author: Matrix
 * @date: 2012-12-3 下午4:43:20
 * 
 */
public class FileEncrypt extends Activity implements OnClickListener {
	private static final String TAG = "PhotoActivity";

	private GridView mGridView = null;

	private TextView mTitleView = null;

	private Button mSelectBtn = null;

	private Button mEncryBtn = null;

	private TextView backTv = null;

	private FileScanner mFileScanner = null;

	private FileEncryptAdapter mPhotoAdapter = null;

	private ProgressDialog mProgressDialog = null;

	private final static int SCAN_PHOTO = 1;

	private int count = 0;

	private String folderPath = "";

	private String folderName = "";

	private boolean allSelected = false;

	private SelectAllListener mOnSelectAllListener = null;

	private FileHandleThreadPool<Callable<IFuture>> mFileHandleThreadPool = FileHandleThreadPool.getInstance();

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.layout_file_encrypt);
		mGridView = (GridView) findViewById(R.id.photo_gridview);
		mTitleView = (TextView) findViewById(R.id.title_tv);
		backTv = (TextView) findViewById(R.id.back_tv);
		backTv.setOnClickListener(this);
		mSelectBtn = (Button) findViewById(R.id.select_btn);
		mSelectBtn.setOnClickListener(this);
		mEncryBtn = (Button) findViewById(R.id.encry_btn);
		mEncryBtn.setOnClickListener(this);
		Intent intent = getIntent();
		count = intent.getIntExtra("count", 0);
		folderPath = intent.getStringExtra("folderpath");
		folderName = intent.getStringExtra("folderName");
		mTitleView.setText(folderName + "(" + count + ")");
		mPhotoAdapter = new FileEncryptAdapter(this, MainApp.getFinalBitmap());
		mGridView.setAdapter(mPhotoAdapter);
		mOnSelectAllListener = mPhotoAdapter.onSelectAllListener;
		mFileScanner = new FileScanner(null);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		scanPackage();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		if (this.mPhotoAdapter != null) {
			mPhotoAdapter.clearView();
		}
		MainApp.getFinalBitmap().clearMemeoryCache();
	}

	/**
	 * 扫描当前文件夹
	 */
	public void scanPackage() {
		Thread fileScanThread = new Thread() {
			@Override
			public void run() {
				File[] fileArray = mFileScanner.findFilesByParentFolder(folderPath);
				Message msg = handler.obtainMessage();
				msg.obj = fileArray;
				msg.what = SCAN_PHOTO;
				handler.sendMessage(msg);
			}
		};
		fileScanThread.start();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.select_btn:
			if (!allSelected) {
				this.mOnSelectAllListener.onSelectAll();
				mSelectBtn.setText("取消");
				allSelected = true;
			} else {
				this.mOnSelectAllListener.onUnSelectAll();
				mSelectBtn.setText("全选");
				allSelected = false;
			}
			break;
		case R.id.encry_btn:
			encryExcute();
			break;
		case R.id.back_tv:
			finish();
			break;
		}

	}

	/**
	 * 加密操作,开启线程池，开启任务队列
	 */
	public void encryExcute() {
		int selectObjectSize = mPhotoAdapter.getSelectedObject().size();
		if (selectObjectSize == 0) {
			Toast.makeText(this, "请选择需要加密的图片", Toast.LENGTH_SHORT).show();
			return;
		}
		loadProgressDialog(selectObjectSize);// 加载进度条
		Thread encryThread = new Thread() {// 开启加密线程
			@Override
			public void run() {
				List<String> photoList = mPhotoAdapter.getSelectedObject();
				for (String path : photoList) {
					FileEncryptTask encryptTask = new FileEncryptTask(path, FileConfig.FILE_TYPE_IMAGE);
					mFileHandleThreadPool.setTaskList(encryptTask);
				}
				photoList.clear();
				mFileHandleThreadPool.mhandler = handler;
				mFileHandleThreadPool.startEncrypt();
			}
		};
		encryThread.start();
	}

	private Handler handler = new Handler() {
		private int count = 1;

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SCAN_PHOTO:
				mPhotoAdapter.setData((File[]) msg.obj);
				mPhotoAdapter.notifyDataSetChanged();
				break;
			case FileHandleThreadPool.COUNTING:
				if (mProgressDialog != null) {
					mProgressDialog.setProgress(count++);
				}
				break;
			case FileHandleThreadPool.TASK_FINISH:
				if (mProgressDialog != null) {
					mProgressDialog.dismiss();
					count = 0;
					// scanPackage();// 刷新本目录
					Intent intent = new Intent();
					intent.setClass(FileEncrypt.this, SafeAlbum.class);
					startActivity(intent);
					finish();
					break;
				}
			}
		}
	};

	/**
	 * 弹出进度条
	 * 
	 * @param taskNum
	 */
	public void loadProgressDialog(int taskNum) {
		mProgressDialog = new ProgressDialog(this);
		// 设置进度条风格，风格为长形
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// 设置ProgressDialog 标题
		mProgressDialog.setTitle("提示");
		// 设置ProgressDialog 提示信
		mProgressDialog.setMessage("正在加密文件");
		// 设置ProgressDialog 标题图标
		// mProgressDialog.setIcon(R.drawable.img2);
		mProgressDialog.setMax(taskNum);
		// 设置ProgressDialog 进度条进度
		// mProgressDialog.setProgress(50);
		// 设置ProgressDialog 的进度条是否不明确
		mProgressDialog.setIndeterminate(false);
		// 设置ProgressDialog 是否可以按退回按键取消
		mProgressDialog.setCancelable(true);
		// 让ProgressDialog显示
		mProgressDialog.show();

	}

}
